package com.caotinging.taskdemo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.caotinging.taskdemo.SchedulingRunnable;
import com.caotinging.taskdemo.config.CronTaskRegistrar;

@RestController
public class TaskController {

	@Autowired
	private  CronTaskRegistrar cronTaskRegistrar;
	
	@RequestMapping(value="/addTask",method=RequestMethod.POST)
	public String addTask(String taskname,String cron){
		 SchedulingRunnable task = new SchedulingRunnable("demoTask", "taskNoParams", null);
		cronTaskRegistrar.addCronTask(task, "0/10 * * * * ?");
		return "success";
	}
	
	@RequestMapping(value="/removeTask",method=RequestMethod.POST)
	public String removeTask(String taskname,String cron){
		SchedulingRunnable task = new SchedulingRunnable("demoTask", "taskNoParams", null);
		cronTaskRegistrar.removeCronTask(task);
		return "success";
	}
}
